import { createArray } from "@/utils/index.js";
import {
  mapCreateMergeHeaderTable,
  mapCreateMergeMainTable,
  mapCreateTable,
} from "./virtual-table/helpers";
import mockHeaderData from "./mockHeaderData.json";
import mockMainData from './mockMainData.json'

class TableUtil {
  constructor() {
    this.rowList = []
    this.columnList = []
    this.startCol = 3
  }

  // 计算并创建要合并的表头数据
  calculateMergeHeaderTable () {
    const self = this
    const headerTableData = mapCreateMergeHeaderTable({
      data: mockHeaderData,
      mapCreateColumn: ({ columnLen }) => {
        return createArray(columnLen, (index) => {
          return {
            title: `${index}-title`,
            field: `${index}-field`,
            width: 120,
          };
        });
      },
      mapCreateData: ({ data, rowIndex, columnIndex }) => {
        const key = `${columnIndex}-field`;
        const value = data.name;
        return {
          key,
          value,
        };
      },
      hooks: {
        mounted () {
          self.columnList = this.leafNodes.slice(3)
        }
      }
    });
    this.headerTableData = headerTableData
  }

  // 计算并创建要合并的表体数据
  calculateMergeMainTable () {
    const self = this
    const mainTableData = mapCreateMergeMainTable({
      data: mockMainData,
      mapCreateColumn: ({ columnLen }) => {
        return createArray(columnLen, (index) => {
          return {
            title: `${index}-title`,
            field: `${index}-field`,
            width: 120,
          };
        });
      },
      mapCreateData: ({ data, rowIndex, columnIndex }) => {
        const key = `${columnIndex}-field`;
        const value = data.name;
        return {
          key,
          value,
        };
      },
      hooks: {
        mounted () {
          self.rowList = this.leafNodes
        }
      }
    });
    this.mainTable1Data = mainTableData
  }

  // 计算并创建不需要合并的表体数据
  calculateNoMergeMainTable ({ slots }) {
    const { rowList, columnList, startCol } = this
    const mainTableData = mapCreateTable({
      data: {
        rowList,
        columnList
      },
      startCol,
      mapCreateColumn: ({ columnLen }) => {
        return createArray(columnLen, (index) => {
          return {
            title: `${index + startCol}-title`,
            field: `${index + startCol}-field`,
            width: 120,
            slots: {
              default: slots
            }
          };
        });
      },
      mapCreateData: ({ row, rowIndex, column, columnIndex }) => {
        const key = `${columnIndex + startCol}-field`
        const value = row
        return {
          key,
          value
        }
      }
    })
    this.mainTable2Data = mainTableData
  }

  // 将表体的两份数据合并
  mergeMainTableData () {
    const {
      columns: main1Columns,
      data: main1Data,
      mergeCells: main1MergeCells
    } = this.mainTable1Data

    const {
      columns: main2Columns,
      data: main2Data,
      mergeCells: main2MergeCells
    } = this.mainTable2Data
    const mergeCells = handleMergeCells(main1MergeCells, main2MergeCells)
    const columns = handleMergeColumns(main1Columns, main2Columns)
    const data = handleMergeData(main1Data, main2Data)

    function handleMergeCells (cells1, cells2) {
      return [...cells1, ...cells2]
    }

    function handleMergeColumns (columns1, columns2) {
      return [...columns1, ...columns2]
    }

    function handleMergeData (data1, data2) {
      return data1.reduce((p, c, index) => {
        p.push({
          ...c,
          ...data2[index]
        })
        return p
      }, [])
    }

    this.mainTableData = {
      data,
      columns,
      mergeCells
    }
  }

  // 获取表格数据
  getData () {
    return {
      headerTableData: this.headerTableData,
      mainTableData: this.mainTableData
    }
  }
}

export default TableUtil